﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public class TimingProject
    {
        private readonly Chain.IDAL.TimingProject dal = new Chain.IDAL.TimingProject();

        public int Add(Chain.Model.TimingProject model)
        {
            return this.dal.Add(model);
        }

        public List<Chain.Model.TimingProject> DataTableToList(DataTable dt)
        {
            List<Chain.Model.TimingProject> list = new List<Chain.Model.TimingProject>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.TimingProject item = new Chain.Model.TimingProject();
                    if ((dt.Rows[i]["ProjectID"] != null) && (dt.Rows[i]["ProjectID"].ToString() != ""))
                    {
                        item.ProjectID = int.Parse(dt.Rows[i]["ProjectID"].ToString());
                    }
                    if ((dt.Rows[i]["ProjectName"] != null) && (dt.Rows[i]["ProjectName"].ToString() != ""))
                    {
                        item.ProjectName = dt.Rows[i]["ProjectName"].ToString();
                    }
                    if ((dt.Rows[i]["ProjectCategoryID"] != null) && (dt.Rows[i]["ProjectCategoryID"].ToString() != ""))
                    {
                        item.ProjectCategoryID = int.Parse(dt.Rows[i]["ProjectCategoryID"].ToString());
                    }
                    if ((dt.Rows[i]["ProjectRulesID"] != null) && (dt.Rows[i]["ProjectRulesID"].ToString() != ""))
                    {
                        item.ProjectRulesID = int.Parse(dt.Rows[i]["ProjectRulesID"].ToString());
                    }
                    if ((dt.Rows[i]["ProjectAddTime"] != null) && (dt.Rows[i]["ProjectAddTime"].ToString() != ""))
                    {
                        item.ProjectAddTime = DateTime.Parse(dt.Rows[i]["ProjectAddTime"].ToString());
                    }
                    if ((dt.Rows[i]["ProjectShopID"] != null) && (dt.Rows[i]["ProjectShopID"].ToString() != ""))
                    {
                        item.ProjectShopID = int.Parse(dt.Rows[i]["ProjectShopID"].ToString());
                    }
                    if ((dt.Rows[i]["ProjectUserID"] != null) && (dt.Rows[i]["ProjectUserID"].ToString() != ""))
                    {
                        item.ProjectUserID = int.Parse(dt.Rows[i]["ProjectUserID"].ToString());
                    }
                    if ((dt.Rows[i]["ProjectRemark"] != null) && (dt.Rows[i]["ProjectRemark"].ToString() != ""))
                    {
                        item.ProjectRemark = dt.Rows[i]["ProjectRemark"].ToString();
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int ProjectID)
        {
            Chain.BLL.OrderTime time = new Chain.BLL.OrderTime();
            if (time.ExistsProject(ProjectID))
            {
                return false;
            }
            Chain.BLL.MemStorageTiming timing = new Chain.BLL.MemStorageTiming();
            return (!timing.ExistsProject(ProjectID) && this.dal.Delete(ProjectID));
        }

        public bool DeleteList(string ProjectIDlist)
        {
            return this.dal.DeleteList(ProjectIDlist);
        }

        public bool Exists(int ProjectID)
        {
            return this.dal.Exists(ProjectID);
        }

        public bool ExistsCategory(int CategoryID)
        {
            return this.dal.ExistsCategory(CategoryID);
        }

        public bool ExistsRules(int RulesID)
        {
            return this.dal.ExistsRules(RulesID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListSP(int MemID, int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListSP(MemID, PageSize, PageIndex, out resCount, strWhere);
        }

        public Chain.Model.TimingProject GetModel(int ProjectID)
        {
            return this.dal.GetModel(ProjectID);
        }

        public List<Chain.Model.TimingProject> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public bool Update(Chain.Model.TimingProject model)
        {
            return this.dal.Update(model);
        }
    }
}

